var myBasePicker = myApp.directive('myBasePicker', ['$rootScope', '$timeout', '$parse', function ($rootScope, $timeout, $parse) {
    return {
        restrict: 'E',
        scope: {
            ngModel: '=',
            csBindValue: '=?csBindValue',
            csOption: '=option'
        },
        link: function ($scope, element, attrs) {
            var defaultOption = { disabled: false, multiple: false, bindType: 'object' };
            angular.extend(defaultOption, $scope.csOption);


            $scope.option = defaultOption;

            if (!angular.isDefined($scope.option.multiple)) {
                $scope.option.multiple = false;
            }

            if (!angular.isDefined($scope.option.disabled)) {
                $scope.option.disabled = false;
            }

            var bindKey = $scope.option.bindType == 'key';//单选，绑定Id值


            $scope.item = {
                selecteds: []
            };
            $scope.$watch('ngModel', function () {
                if (bindKey) {
                    if (angular.isDefined($scope.ngModel) && $scope.ngModel != '') {
                        $scope.item.selecteds = [$scope.getObj($scope.ngModel, $scope.csBindValue)];
                    } else {
                        $scope.item.selecteds = [];
                    }
                } else {
                    if (angular.isDefined($scope.ngModel)) {
                        if ($scope.option.multiple && angular.isArray($scope.ngModel)) {
                            $scope.item.selecteds = $scope.ngModel;
                        } else if (angular.isObject($scope.ngModel)) {
                            $scope.item.selecteds = [$scope.ngModel];

                        }
                    }
                }
            });
            if (bindKey) {
                $scope.$watch('csBindValue', function () {
                    if (angular.isDefined($scope.ngModel) && $scope.ngModel != '') {
                        $scope.item.selecteds = [$scope.getObj($scope.ngModel, $scope.csBindValue)];
                    }
                });
            }

            var valueChange = function () {
                if (bindKey) {
                    if (!$scope.option.multiple) {
                        if ($scope.item.selecteds.length > 0) {
                            $scope.ngModel = $scope.option.getKey($scope.item.selecteds[0]);
                            $scope.csBindValue = $scope.option.getText($scope.item.selecteds[0]);
                        } else {
                            $scope.ngModel = '';
                            $scope.csBindValue = '';
                        };
                    }
                } else {
                    if ($scope.option.multiple) {
                        $scope.ngModel = $scope.item.selecteds;
                    } else {
                        if ($scope.item.selecteds.length > 0) {
                            $scope.ngModel = $scope.item.selecteds[0];
                        } else {
                            $scope.ngModel = undefined;
                        }
                    }
                }

                if (attrs.ngChange) {
                    $parse(attrs.ngChange)($scope.$parent);
                }
            };

            $scope.getText = function (select) {
                return $scope.option.getText(select);

            };

            $scope.getObj = function (key, value) {
                return $scope.option.getObj(key, value);
            };


            $scope.selectedRemove = function (select) {
                var index = $scope.item.selecteds.indexOf(select);
                $scope.item.selecteds.splice(index, 1);
                valueChange();
            }

            $scope.openSelect = function () {
                $rootScope.settings.open(
                    $scope.option.modalUrl,
                    null,
                    { title: $scope.option.modalTitle, action: 'select', item: $scope.item, option: $scope.option },
                    function (selecteds) {
                        if ($scope.option.multiple) {
                            angular.forEach(selecteds, function (select) {
                                $scope.item.selecteds.push(select);
                            });

                        } else {
                            $scope.item.selecteds = selecteds;

                        }
                        valueChange();

                    }, function () {

                    }
                );
            };
        },
        controller: ['$rootScope', '$scope', '$http', function ($rootScope, $scope, $http) {

        }],
        // templateUrl: './templates/select.html'
        template: '<div class="input-group">'+
                        '<div class="" style="width:100%">'+
                            '<div title="点击选择" ng-disabled="option.disabled" class="list-group-item text-right" style="padding:0px;">'+
                                '<div class="pull-left label" style="margin:6px;" ng-repeat="node in item.selecteds">'+
                                    '<span style="color:white; ">'+
                                        '<span> {{getText(node)}}</span><a style="color:white;margin-left:5px;" href="#" ng-if="!option.disabled" class="" ng-click="selectedRemove(node)"><i class="fa fa-times"></i></a>'+
                                    '</span>'+
                                '</div>'+
                                '<span class="input-group-btn">'+
                                    '<button ng-click="openSelect()" ng-disabled="option.disabled" class="btn btn-link" type="button"><i class="fa fa-list-alt"></i></button>'+
                                '</span>'+
                                '<div class="clearfix"></div>'+
                            '</div>'+
                        '</div>'+ 
                    '</div>'
    };
}]);